﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Ashlen.Portal.Controllers;
using MvcContrib.TestHelper;
using System.Web.Mvc;
using NUnit.Framework;
using NUnit.Framework.SyntaxHelpers;
using Rhino.Mocks;
using System.Linq;
using Ashlen.Portal.Core;
using Ashlen.Portal.Core.DataInterfaces;
using Ashlen.Portal.Data.SqlServer;

namespace Ashlen.Portal.Tests.Ashlen.Portal.DataTests
{
    [TestFixture]
    public class WidgetUIDaoTests
    {
        private IWidgetDao _dao;

        [SetUp]
        public void Setup() {
            _dao = new WidgetDao();
        }

        #region Widget Tests
        private ui_Widget CreateWidget() {
            ui_Widget w = new ui_Widget { Name = "testWidget", Url = "http://www.theFijiConnection/widgets/testWidget" ,
                                          Description = "Test Widget",
                                          VersionNo = 1,
                                          UserCreated = "Shailen Sukul",
                                          IsDefault = false,
                                          Body = "<html></html>",
                                          DefaultState = string.Empty,
                                          Icon="http://www.ashlen.com.au/images/icons/testWidget.jpg",
                                          OrderNo=1
            };
            return w;
        }

        [Test]
        public void CanSelectWidget() {
            Guid? id = Guid.Empty;
            try {
                id = _dao.CreateWidget(CreateWidget());
            }
            finally {
                _dao.DeleteWidget(id.Value);
            }
        }

        [Test]
        public void CanCreateWidget() {
            Guid? id = Guid.Empty;
            try {                
                id = _dao.CreateWidget(CreateWidget());
                    Assert.IsNotNull(id);
            }
            finally {
                _dao.DeleteWidget(id.Value);
            }
        }

        [Test]
        public void CanUpdateWidget() {
            Guid? id = Guid.Empty;
            try {
                id = _dao.CreateWidget(CreateWidget());
                Assert.IsNotNull(id);
                ui_Widget widget = _dao.SelectWidget(id, default(string)) as ui_Widget;
                widget.Name = "testWidget2";
                widget.UserUpdated = "Shailen";
                _dao.UpdateWidget(widget);
                widget = _dao.SelectWidget(id, default(string)) as ui_Widget;
                Assert.AreEqual(widget.Name, "testWidget2");
            }
            finally {
                _dao.DeleteWidget(id.Value);
            }
        }

        [Test]
        public void CanDeleteWidget() {
            Guid id = _dao.CreateWidget(CreateWidget());
            _dao.DeleteWidget(id);
        }

        #endregion

        #region Page Tests
        private ui_Page CreatePage() {
            ui_Page page = new ui_Page {
                ID = Guid.NewGuid(),
                VersionNo = 1,
                UserId = Guid.NewGuid(),
                Title = "test page",
                //LayoutType = 1,
                UserCreated = "shailen sukul"
            };
            return page;
        }

        [Test]
        public void CanCreatePage() {
            ui_Page page = CreatePage();
            Guid? id = default(Guid);
            try {                
                id = _dao.CreatePage(page);
            }
            finally {
                _dao.DeletePage(id.Value);
            }
        }

        [Test]
        public void CanUpdatePage() {
            ui_Page page = CreatePage();
            Guid? id = default(Guid);
            try {
                id = _dao.CreatePage(page);
                page = _dao.SelectPage(page.Title, "") as ui_Page;
                //page.LayoutType = 3;
                _dao.UpdatePage(page);
                page = _dao.SelectPage(page.Title, "") as ui_Page;
                //Assert.AreEqual(page.LayoutType, 3);
            }
            finally {
                _dao.DeletePage(id.Value);
            }
        }
        #endregion

        #region WidgetInstance Tests

        private ui_WidgetInstance CreateWidgetInstance() {
            ui_WidgetInstance wi = new ui_WidgetInstance {
                Id = Guid.NewGuid(),
                Title = "My widget instance",
                VersionNo = 1,
                ColumnNo = 1,
                OrderNo = 1,
                UserCreated = "shailen sukul",
                Expanded = false,
                State = "<state></state>"
            };            
            return wi;
        }

        public ui_Widget CreateWidgetInstance_Widget() {
            ui_Widget w = CreateWidget();
            Guid id = _dao.CreateWidget(w);
            return _dao.SelectWidget(id, null) as ui_Widget;
        }
        public ui_Page CreateWidgetInstance_Page() {
            ui_Page page = CreatePage();
            Guid id = _dao.CreatePage(page);
            return _dao.SelectPage(page.Title, "") as ui_Page;
        }

        [Test]
        public void CanCreateWidgetInstance() {
            ui_WidgetInstance wi = CreateWidgetInstance();
            ui_Widget widget = CreateWidgetInstance_Widget();
            ui_Page page = CreateWidgetInstance_Page();

            Guid? id = default(Guid);
            try {
                id = _dao.CreateWidgetInstance(widget.ID, page.ID, wi.ColumnNo, wi.OrderNo, wi.Expanded,
                    wi.Title, wi.State, wi.VersionNo, wi.UserCreated);
            }
            finally {
                _dao.DeleteWidgetInstance(id.Value);
                _dao.DeletePage(page.ID);
                _dao.DeleteWidget(widget.ID);
            }
        }

        [Test]
        [ExpectedException(typeof(ArgumentException))]
        public void CanCreateWidgetInstance2() {
            ui_WidgetInstance wi = CreateWidgetInstance();
            Guid? id = default(Guid);
            try {
                id = _dao.CreateWidgetInstance(Guid.NewGuid(), Guid.NewGuid(), wi.ColumnNo, wi.OrderNo, wi.Expanded,
                    wi.Title, wi.State, wi.VersionNo, wi.UserCreated);
            }
            finally {
            }
        }

        [Test]
        public void CanUpdateWidgetInstance() {
            ui_WidgetInstance wi = CreateWidgetInstance();
            ui_Widget widget = CreateWidgetInstance_Widget();
            ui_Page page = CreateWidgetInstance_Page();

            Guid? id = default(Guid);
            try {
                id = _dao.CreateWidgetInstance(widget.ID, page.ID, wi.ColumnNo, wi.OrderNo, wi.Expanded,
                    wi.Title, wi.State, wi.VersionNo, wi.UserCreated);
                wi = _dao.SelectWidgetInstance(widget.ID, page.ID) as ui_WidgetInstance;
                wi.VersionNo = 4;
                _dao.UpdateWidgetInstance(wi.Id, widget.ID, page.ID, wi.ColumnNo, wi.OrderNo, wi.Expanded,
                    wi.Title, wi.State, wi.VersionNo, wi.UserUpdated);
                wi = _dao.SelectWidgetInstance(widget.ID, page.ID) as ui_WidgetInstance;
                Assert.AreEqual(wi.VersionNo, 4);
            }
            finally {
                _dao.DeleteWidgetInstance(id.Value);
                _dao.DeletePage(page.ID);
                _dao.DeleteWidget(widget.ID);
            }
        }
        #endregion
    }
}
